// pages/Home/content/content.js
var app = getApp();
var ikz = require('../../Common/tools.js');
// var WxParse = require('./../../../pages/wxParse/wxParse.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    article_content: "",
    replydis: 'none', //回复是否显示
    head_info: '', //优选信息
    advert: '', //广告信息
    zanhead: '2', //本条消息是否赞过
    iscollect: 'wei', //本条消息是否赞过
    hid: '', //本条信息id
    inputValue: '', //评论输入框内容
    danmuValue: '',//弹幕输入框内容
    comment_time_info: '', //最新评论信息
    comment_like_info: '', //精彩评论信息
    isallzan: 2, //2可以操作点赞，3不可以，防止重复点击
    iscol: 2, //2可以操作收藏，3不可以，防止重复点击
    imgnum: 1, //banner第几张
    isChecked: false,
    showView: false, //发表弹窗
    showViews:false, //收藏弹窗
    all_img: '',
    isFold: false,
    relevel: '1', //评论级别，1是本条信息（一级评论），2是二级评论，3是三级评论
    mid: '0', //所评论id
    fid: '0', //所评论的父级评论id
    ismore: '2', //查看更多
    remarkshow: 'none', //不回复的情况下 点击遮罩层消失
    like_info: '', //点赞人头像
    remarkshow: 'none', //不回复的情况下 点击遮罩层消失
    disshow: 'none',
    selectid: '',//选择删除的评论id
    selecttyp: '',//选择删除的评论类型
    remarkHeight: '', //获取评论高度
    coldis: 'none',//收藏弹窗是否显示
    head_open_info: '',//相关推荐信息
    danmuList: [],//弹幕列表
    nowpalytime: '',//视频播放进度（取整）
  },
  // 详情页
  toContent: function (e) {
    var that = this;
    var hid = e.currentTarget.dataset.hid;
    wx.navigateTo({
      url: '../pageDetail/pageDetail?' + 'hid=' + hid,
    })

  },
  //获取当前播放时间
  playTime: function (e) {
    var that = this;
    var currentTime = e.detail.currentTime;
    if (currentTime <= 1){
      var nowpalytime = 1;
    }else{
      var nowpalytime = Math.floor(currentTime);
    }
    that.setData({
      nowpalytime: nowpalytime,
    })
  },
  //显示分享
  showShare: function (e) {
    console.log(e)
    var hid = e.currentTarget.dataset.hid;
    var head_info = this.data.head_info;
    for (var i = 0; i < head_info.length; i++) {
      if (hid == head_info[i].head_id) {
        if (head_info[i].isshare == '2') {
          head_info[i].isshare = '3';
        } else {
          head_info[i].isshare = '2';
        }
      } else {
        head_info[i].isshare = '2';
      }
    }
    this.setData({
      head_info: head_info,
    })
  },

  // 详情页点击收藏
  catchcollect: function (e) {
    var coldis = this.data.coldis;
    if (coldis == 'none') {
      coldis = 'block';
    } else {
      coldis = 'none';
    }
    this.setData({
      coldis: coldis,
      showViews: !this.data.showViews,
    })
  },
  // 点击收藏
  collectTop: function (e) {
    var hid = this.data.hid;
    var that = this;
    //防止重复点击
    that.setData({
      iscol: 3,
      showViews: false,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/collectHead';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        wx.showToast({
          image: '../../img/success.png',
          title: '收藏成功！',
          duration: 2000
        });
        that.setData({
          iscollect: 'yi',
          coldis: 'none',
        });
      } else if (res.data.status == 'fails') {
        wx.showToast({
          image: '../../img/write.png',
          title: '已收藏！',
          duration: 2000
        });
        that.setData({
          iscollect: 'yi',
          coldis: 'none',
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iscol: 2,
      });
    });
  },
  // 取消收藏
  collectTops: function (e) {
    var hid = this.data.hid;
    var that = this;
    //防止重复点击
    that.setData({
      iscol: 3,
      showViews: false,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/collectCancle';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        wx.showToast({
          image: '../../img/remove.png',
          title: '取消收藏成功！',
          duration: 2000
        });
        that.setData({
          iscollect: 'wei',
          coldis: 'none',
        });
      } else if (res.data.status == 'fails') {
        wx.showToast({
          image: '../../img/write.png',
          title: '已取消收藏！',
          duration: 2000
        });
        that.setData({
          iscollect: 'wei',
          coldis: 'none',
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iscol: 2,
      });
    });
  },
  //图片点击事件
  showImg: function (event) {
    var src = event.currentTarget.dataset.src; //获取data-src
    var imgList = this.data.all_img; //获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  //  点击三个圆点
  huifushow: function (e) {
    var selectid = e.currentTarget.dataset.mid;
    var selecttyp = e.currentTarget.dataset.typ;
    var that = this;
    var disshows = that.data.disshow;
    if (disshows == 'none') {
      var disshow = 'block'
    } else {
      var disshow = 'none'
    }
    that.setData({
      disshow: disshow,
      selectid: selectid,
      selecttyp: selecttyp,
    })
  },
  // 点击回复按钮
  reply: function (e) {
    var mid = e.currentTarget.dataset.mid;
    var that = this;
    that.setData({
      mid: mid,
      showView: (!that.data.showView),
      remarkshow: 'block',
      disshow: 'none'
    })
  },
  catchtabinput: function () {
    var that = this;
    that.setData({
      remarkshow: 'block'
    })
  },
  // 不回复 点击关闭
  close: function () {
    var that = this;
    that.setData({
      remarkshow: 'none'
    })
  },

  // 点击查看更多评论
  clickmore: function (e) {
    var hid = this.data.hid;
    var that = this;
    var data = {
      hid: hid,
    };
    wx.showLoading({
      title: '刷新中...',
    });
    var path = 'Index/clickMoreCon';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        that.setData({
          comment_time_info: res.data.comment_time_info,
          ismore: 3,
        });
        wx.hideLoading();
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }
    });
  },
  // 点击收回更多评论
  clickless: function (e) {
    var that = this;
    var comment_time_info = that.data.comment_time_info;
    if (comment_time_info.length > 2) {
      comment_time_info.length = 2;
    }

    that.setData({
      comment_time_info: comment_time_info,
      ismore: '2',
    });

  },
  // 点击查看更多子评论
  sonmore: function (e) {
    var mid = e.currentTarget.dataset.mid;
    var typ = e.currentTarget.dataset.typ;
    var that = this;
    var comment_time_info = that.data.comment_time_info;
    var comment_like_info = that.data.comment_like_info;
    var data = {
      mid: mid,
    };
    var path = 'Index/sonMoreCon';
    ikz.request(path, data, true, (res) => {

      if (res.data.status == 'success') {
        if (typ == 'zuixin') {
          for (var i = 0; i < comment_time_info.length; i++) {
            if (mid == comment_time_info[i].com_id) {
              comment_time_info[i].com_more = 3;
              comment_time_info[i].com_time_son_info = res.data.com_time_son_info;
              comment_time_info[i].com_count = res.data.com_time_son_info.length;
            }
          }
        } else {
          for (var i = 0; i < comment_like_info.length; i++) {
            if (mid == comment_like_info[i].com_id) {
              comment_like_info[i].com_more = 3;
              comment_like_info[i].com_like_son_info = res.data.com_time_son_info;
              comment_like_info[i].com_count = res.data.com_time_son_info.length;
            }
          }
        }
        that.setData({
          comment_time_info: comment_time_info,
          comment_like_info: comment_like_info,
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }
    });
  },
  // 点击收回更多子评论
  sonless: function (e) {
    var mid = e.currentTarget.dataset.mid;
    var typ = e.currentTarget.dataset.typ;
    var that = this;
    var comment_time_info = that.data.comment_time_info;
    var comment_like_info = that.data.comment_like_info;
    if (typ == 'zuixin') {
      for (var i = 0; i < comment_time_info.length; i++) {
        if (mid == comment_time_info[i].com_id) {
          comment_time_info[i].com_more = 2;
          if (comment_time_info[i].com_time_son_info.length > 2) {
            comment_time_info[i].com_time_son_info.length = 3;
          }
        }
      }
    } else {
      for (var i = 0; i < comment_like_info.length; i++) {
        if (mid == comment_like_info[i].com_id) {
          comment_like_info[i].com_more = 2;
          if (comment_like_info[i].com_like_son_info.length > 2) {
            comment_like_info[i].com_like_son_info.length = 3;
          }
        }
      }
    }
    that.setData({
      comment_time_info: comment_time_info,
      comment_like_info: comment_like_info,
    });

  },
  //溢出隐藏样式
  flodFn: function () {
    console.log('改变样式')
    this.setData({
      isFold: !this.isFold
    });
  },

  //banner变化
  swiperChange: function (e) {
    var imgnum = e.detail.current + 1;
    this.setData({
      imgnum: imgnum,
    });
  },
  serviceSelection: function () {
    this.setData({
      isChecked: true
    })
  },
  
  //获取评论输入框内容
  bindKeyInput: function (e) {
    var that = this;
    this.setData({
      inputValue: e.detail.value,
    })
  },
  //发表评论
  makeConComment: function (e) {
    var that = this;
    var com_content = that.data.inputValue;
    var comment_time_info = that.data.comment_time_info;
    var comment_like_info = that.data.comment_like_info;
    var head_info = that.data.head_info;
    //判断评论不能为空
    if (com_content == '') {
      wx.showToast({
        title: '请填写评论',
        duration: 2000
      });
      return false;
    }
    //防止重复发表
    that.setData({
      remarkshow: 'none'
    })
    var hid = that.data.hid;
    var mid = that.data.mid;
    var data = {
      mid: mid,
      hid: hid,
      com_content: com_content,
    }
    var path = 'Index/makeConComment';
    ikz.request(path, data, true, (res) => {
      if (res.data.status == 'success') {
        //最新评论
        if (comment_time_info.length) {
          if (mid == '0') {//一级评论
            comment_time_info.unshift(res.data.comment_info);
          } else {//二级评论
            for (var i = 0; i < comment_time_info.length; i++) {
              if (mid == comment_time_info[i].com_id) {
                comment_time_info[i].com_time_son_info.unshift(res.data.comment_info);
                comment_time_info[i].com_count = parseInt(comment_time_info[i].com_count) + 1;
              }
            }
          }
        } else {//无最新评论
          comment_time_info[0] = res.data.comment_info;
        }
        //精彩评论的二级评论
        if (comment_like_info.length) {
          if (mid != '0') {
            for (var i = 0; i < comment_like_info.length; i++) {
              if (mid == comment_like_info[i].com_id) {
                comment_like_info[i].com_like_son_info.unshift(res.data.comment_info);
                comment_like_info[i].com_count = parseInt(comment_like_info[i].com_count) + 1;
              }
            }
          }
        }
        //总评论数
        head_info.count_comment = parseInt(head_info.count_comment) + 1;
        wx.showToast({
          title: '发表成功！',
          duration: 2000
        });
        that.setData({
          comment_time_info: comment_time_info,
          comment_like_info: comment_like_info,
          head_info: head_info,
          showView: false,
          inputValue: '',
        })
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }

    });
  },
  //本条消息点赞
  goHeadLike: function (e) {
    var hid = this.data.hid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      isallzan: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/goHeadLike';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        head_info.head_like = parseInt(head_info.head_like) + 1;
        that.setData({
          zanhead: '3',
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }
      that.setData({
        isallzan: 2,
      });
    });
  },
  //本条取消消息点赞
  noHeadLike: function (e) {
    var hid = this.data.hid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      isallzan: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/noHeadLike';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        head_info.head_like = parseInt(head_info.head_like) - 1;
        that.setData({
          zanhead: '2',
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }
      that.setData({
        isallzan: 2,
      });
    });
  },
  //评论点赞
  goComLike: function (e) {
    var that = this;
    var mid = e.currentTarget.dataset.mid; //本评论id
    var typ = e.currentTarget.dataset.typ;
    var comment_time_info = that.data.comment_time_info;
    var comment_like_info = that.data.comment_like_info;
    var data = {
      comid: mid,
    };
    that.setData({
      isallzan: 3,
    });
    var path = 'Index/goComLike';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        if (typ == 'zuixin') {
          for (var i = 0; i < comment_time_info.length; i++) {
            if (mid == comment_time_info[i].com_id) {
              comment_time_info[i].islike = 3;
              comment_time_info[i].com_like = parseInt(comment_time_info[i].com_like) + 1;
            }
          }
        } else {
          for (var i = 0; i < comment_like_info.length; i++) {
            if (mid == comment_like_info[i].com_id) {
              comment_like_info[i].islike = 3;
              comment_like_info[i].com_like = parseInt(comment_like_info[i].com_like) + 1;
            }
          }
        }

        that.setData({
          comment_time_info: comment_time_info,
          isallzan: 2,
          comment_like_info: comment_like_info,
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        });
      }
    });
  },
  //评论取消点赞
  noComLike: function (e) {
    var that = this;
    var mid = e.currentTarget.dataset.mid; //本评论id
    var typ = e.currentTarget.dataset.typ;
    var comment_time_info = that.data.comment_time_info;
    var comment_like_info = that.data.comment_like_info;
    var data = {
      comid: mid,
    };
    that.setData({
      isallzan: 3,
    });
    var path = 'Index/noComLike';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        if (typ == 'zuixin') {
          for (var i = 0; i < comment_time_info.length; i++) {
            if (mid == comment_time_info[i].com_id) {
              comment_time_info[i].islike = 2;
              comment_time_info[i].com_like = parseInt(comment_time_info[i].com_like) - 1;
            }
          }

        } else {
          for (var i = 0; i < comment_like_info.length; i++) {
            if (mid == comment_like_info[i].com_id) {
              comment_like_info[i].islike = 2;
              comment_like_info[i].com_like = parseInt(comment_like_info[i].com_like) - 1;
            }
          }

        }

        that.setData({
          comment_time_info: comment_time_info,
          comment_like_info: comment_like_info,
          isallzan: 2,
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        });
      }
    });
  },
  //删除评论
  delCom: function (e) {
    var that = this;
    that.setData({
      disshow: 'none'
    })
    var mid = e.currentTarget.dataset.mid; //本评论id
    var typ = e.currentTarget.dataset.typ;
    var fid = e.currentTarget.dataset.fid;
    var relevel = e.currentTarget.dataset.relevel;
    var comment_time_info = that.data.comment_time_info;
    var comment_like_info = that.data.comment_like_info;
    var hid = this.data.hid;

    var data = {
      comid: mid,
      hid: hid,
    };
    wx.showModal({
      title: '提示',
      content: '您确认删除评论吗？',
      success: function (res) {
        if (res.confirm) {
          var path = 'Index/delCom';
          ikz.request(path, data, true, (res) => {
            console.log(res);
            if (res.data.status == 'success') {
              wx.showToast({
                title: '删除评论成功！',
                duration: 2000
              });
              if (typ == 'zuixin') {
                if (relevel == '1') {
                  var z = -1;
                  for (var i = 0; i < comment_time_info.length; i++) {
                    if (mid == comment_time_info[i].com_id) {
                      z = i;
                    }
                  }
                  if (z > -1) {
                    comment_time_info.splice(z, 1);
                  };
                } else {
                  var z = -1;
                  var x = -1;
                  for (var i = 0; i < comment_time_info.length; i++) {
                    if (fid == comment_time_info[i].com_id) {
                      comment_time_info[i].com_count = parseInt(comment_time_info[i].com_count) - 1;
                      z = i;
                      for (var k = 0; k < comment_time_info[i].com_time_son_info.length; k++) {
                        if (mid == comment_time_info[i].com_time_son_info[k].com_id) {
                          x = k;
                        }
                      }
                    }
                  }
                  if (x > -1) {
                    comment_time_info[z].com_time_son_info.splice(x, 1);
                  };
                }
              } else {
                if (relevel == '1') {
                  var z = -1;
                  for (var i = 0; i < comment_like_info.length; i++) {
                    if (mid == comment_like_info[i].com_id) {
                      z = i;
                    }
                  }
                  if (z > -1) {
                    comment_like_info.splice(z, 1);
                  };
                } else {
                  var z = -1;
                  var x = -1;
                  for (var i = 0; i < comment_like_info.length; i++) {
                    if (fid == comment_like_info[i].com_id) {
                      comment_like_info[i].com_count = parseInt(comment_like_info[i].com_count) - 1;
                      z = i;
                      for (var k = 0; k < comment_like_info[i].com_like_son_info.length; k++) {
                        if (mid == comment_like_info[i].com_like_son_info[k].com_id) {
                          x = k;
                        }
                      }
                    }
                  }
                  if (x > -1) {
                    comment_like_info[z].com_like_son_info.splice(x, 1);
                  };
                }
              }
              that.setData({
                comment_time_info: comment_time_info,
                comment_like_info: comment_like_info,
              })
            } else if (res.data.status == 'haveson') {
              wx.showModal({
                title: '提示',
                content: '本评论有子评论，不能删！',
                showCancel: false
              })
            } else if (res.data.status == 'nochom') {
              wx.showModal({
                title: '提示',
                content: '不是本人评论，不能删！',
                showCancel: false
              })
            } else {
              wx.showModal({
                title: '加载失败',
                content: '请稍后重试！',
                showCancel: false
              })
            }

          });
        } else if (res.cancel) {
          return false;
        }
      }
    })

  },
  //相关推荐更多
  toRelation: function (e) {
    var that = this;
    var hid = that.data.hid;
    wx.navigateTo({
      url: '../relation/relation?' + 'hid=' + hid,
    })

  },
  /**
   * 生命周期函数--监听页面加载s
   */
  onLoad: function (options) {
    console.log(options)
    var hid = options.hid;
    ikz.over(this);
    this.setData({
      hid: hid,
      remarkshow: 'none',
      disshow: 'none',
    });
    var query = wx.createSelectorQuery();
    //选择id
    var that = this;
    query.select('.jingcaimark').boundingClientRect(function (rect) {
      console.log(rect)
      // that.setData({
      // height: rect.offsetHeight + 'px'
      // })
    }).exec();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.videoContext = wx.createVideoContext('myVideo');
    // this.videoContext.requestFullScreen();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var hid = this.data.hid;
    var that = this;
    var data = {
      hid: hid,
    }
    if (app.globalData.openUserInfo == '') {
      var islogin = false;
    } else {
      var islogin = true;
    }
    var path = 'Index/content';
    ikz.request(path, data, islogin, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        if (res.data.head_info.head_img) {
          var all_img = res.data.head_info.head_img;
        } else {
          var all_img = [];
        }
        that.setData({
          all_img: all_img,
          // article_content: WxParse.wxParse('article_content', 'html', that.data.article_content, this, 5),

          head_info: res.data.head_info,
          like_info: res.data.like_info,
          advert_info: res.data.advert_info,
          hid: hid,
          iscollect: res.data.iscollect,
          comment_time_info: res.data.comment_time_info,
          comment_like_info: res.data.comment_like_info,
          head_open_info: res.data.head_open_info,
          danmuList: res.data.danmuList,
          zanhead: res.data.zanhead,
          isallzan: 2,
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }

    });

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log(111)


  },
  getRandomColor() {
    let rgb = []
    for (let i = 0; i < 3; ++i) {
      let color = Math.floor(Math.random() * 256).toString(16)
      color = color.length == 1 ? '0' + color : color
      rgb.push(color)
    }
    return '#' + rgb.join('')
  },
  // danmuValue: '',
  danmuInputBlur: function (e) {
    this.setData({
      danmuValue: e.detail.value
    })
  },
  // 发送弹幕
  bindSendDanmu: function (e) {
    var that = this;
    var nowpalytime = that.data.nowpalytime;
    var hid = that.data.hid;
    var danmuValue = that.data.danmuValue;
    var color = that.getRandomColor();
    var data = {
      hid: hid,
      nowpalytime: nowpalytime,
      color: color,
      danmuValue: danmuValue,
    }
    var path = 'Index/sendDanmu';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      that.setData({
        danmuValue:'',
      })
      if (res.data.status == 'success') {

        that.videoContext.sendDanmu({
          text: danmuValue,
          color: color,
         
        });

      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }

    });
  },
  //转发某条信息
  onShareAppMessage: function (res) {
    var hid = this.data.hid;
    this.setData({
      coldis: 'none',
      showViews: false,
    });
    return {
      title: '',
      path: '/pages/Home/pageDetail/pageDetail?hid=' + hid,
    }
  }
})